package com.yxj.demo_01

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.databinding.DataBindingUtil
import com.yxj.demo_01.databinding.FragmentFirstBinding
import com.yxj.demo_01.model.User

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class FirstFragment : Fragment() {

//    private lateinit var editText: EditText
//    private lateinit var userName: TextView

    private lateinit var binding: FragmentFirstBinding

    private val user: User = User("喜羊羊与灰太狼")

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_first, container, false)

        // Inflate the layout for this fragment
        // return inflater.inflate(R.layout.fragment_first, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.buttonFirst.setOnClickListener {
            addUserName(view)
        }

//        view.findViewById<Button>(R.id.button_first).setOnClickListener {
//            //            findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
//            addUserName(view)
//        }

//        editText = view.findViewById(R.id.edit_name)
//        userName = view.findViewById(R.id.user_name)

//        userName.setOnClickListener { updateUserName(view) }
        binding.userName.setOnClickListener { updateUserName(view) }

//        binding.userName = user.username
    }

    @SuppressLint("ServiceCast")
    private fun addUserName(view: View) {

        binding.apply {
            binding.userName.text = binding.editName.text
//            user.username?.userName=
            binding.userName.visibility = View.VISIBLE
            binding.editName.visibility = View.GONE
            binding.buttonFirst.visibility = View.GONE
        }


        val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    @SuppressLint("ServiceCast")
    private fun updateUserName(view: View) {

        binding.userName.text = binding.editName.text

        binding.userName.visibility = View.VISIBLE
        binding.editName.visibility = View.VISIBLE
        binding.buttonFirst.visibility = View.VISIBLE
        binding.editName.requestFocus()

        val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput( binding.editName, 0)
    }
}